package com.yanqu.road.dataHelper.action.excel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekEventData;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekStudentData;
import com.yanqu.road.entity.activity.shrek.DouLuoShrekUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Shrek extends BaseRepair {

    public Shrek(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLog();
        DataMgr.addData(listMap);
    }


    private List<Map<String, String>> getLog() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_douluo_shrek";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoShrekUserData data = parseData(rs);
                int count = data.getStudentCount();
                int calCount = 0;
                Map<Integer, Integer> titleCountMap = data.getSolderTitleCountMap();
                for(int titleCount : titleCountMap.values()){
                    calCount += titleCount;
                }
                Map<Integer, Integer> dieCountMap = data.getDieCountMap();
                for(int dieCount : dieCountMap.values()){
                    calCount += dieCount;
                }
                Map<Integer, DouLuoShrekStudentData> studentDataMap = data.getStudentMap();
                calCount += studentDataMap.size();
                if(count < calCount){
                    Map<String,String> temp = new ConcurrentHashMap<>();
                    temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                    temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                    temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                    temp.put("count",""+count);
                    temp.put("calCount",""+calCount);
                    dataList.add(temp);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    protected DouLuoShrekUserData parseData(ResultSet rs) throws SQLException {
        DouLuoShrekUserData data = new DouLuoShrekUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setIntegral(rs.getLong("integral"));
        data.setProduceTime(rs.getLong("produceTime"));
        data.setStudentCount(rs.getInt("studentCount"));
        data.setBossId(rs.getInt("bossId"));
        data.setBossBlood(new BigInteger(rs.getString("bossBlood")));
        data.setBossKillTimes(rs.getInt("bossKillTimes"));
        data.setLastEventTime(rs.getLong("eventTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        String buildingStr  = rs.getString("buildingLevelData");
        if(!StringUtils.isNullOrEmpty(buildingStr)){
            Map<Integer, Integer> result = JSON.parseObject(buildingStr, new TypeReference<Map<Integer,Integer>>() {});
            if(result != null){
                data.setBuildingLevelMap(result);
            }
        }
        String buildingLevelUpTimeStr  = rs.getString("buildingLevelUpTimeData");
        if(!StringUtils.isNullOrEmpty(buildingLevelUpTimeStr)){
            Map<Integer, Map<Integer, Long>> result = JSON.parseObject(buildingLevelUpTimeStr, new TypeReference<Map<Integer, Map<Integer, Long>>>() {});
            if(result != null){
                data.setBuildingLevelUpTimeMap(result);
            }
        }
        String skillStr  = rs.getString("skillLevelData");
        if(!StringUtils.isNullOrEmpty(skillStr)){
            Map<Integer, Integer> result = JSON.parseObject(skillStr, new TypeReference<Map<Integer,Integer>>() {});
            if(result != null){
                data.setSkillLevelMap(result);
            }
        }
        String studentStr  = rs.getString("studentData");
        if(!StringUtils.isNullOrEmpty(studentStr)){
            Map<Integer, DouLuoShrekStudentData> result = JSON.parseObject(studentStr, new TypeReference<Map<Integer, DouLuoShrekStudentData>>() {});
            if(result != null){
                data.setStudentMap(result);
            }
        }
        String solderStr  = rs.getString("solderData");
        if(!StringUtils.isNullOrEmpty(solderStr)){
            Map<Integer, String> result = JSON.parseObject(solderStr, new TypeReference<Map<Integer, String>>() {});
            if(result != null){
                data.setSolderData(result);
            }
        }
        String eventStr  = rs.getString("eventData");
        if(!StringUtils.isNullOrEmpty(eventStr)){
            Map<Integer, DouLuoShrekEventData> result = JSON.parseObject(eventStr, new TypeReference<Map<Integer, DouLuoShrekEventData>>() {});
            if(result != null){
                data.setEventMap(result);
            }
        }
        String titleCountStr  = rs.getString("titleCountData");
        if(!StringUtils.isNullOrEmpty(titleCountStr)){
            Map<Integer, Integer> result = JSON.parseObject(titleCountStr, new TypeReference<Map<Integer, Integer>>() {});
            if(result != null){
                data.setSolderTitleCountMap(result);
            }
        }
        String disappearStudentStr  = rs.getString("cacheDisappearStudent");
        if(!StringUtils.isNullOrEmpty(disappearStudentStr)){
            Map<Integer, DouLuoShrekStudentData> result = JSON.parseObject(disappearStudentStr, new TypeReference<Map<Integer, DouLuoShrekStudentData>>() {});
            if(result != null){
                data.setCacheDisappearMap(result);
            }
        }
        String dieCountDataStr  = rs.getString("dieCountData");
        if(!StringUtils.isNullOrEmpty(dieCountDataStr)){
            Map<Integer, Integer> result = JSON.parseObject(dieCountDataStr, new TypeReference<Map<Integer, Integer>>() {});
            if(result != null){
                data.setDieCountMap(result);
            }
        }
        return data;
    }

}
